package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.domain.SignState;
import cn.wolfcode.trip.base.domain.UserIntegral;
import cn.wolfcode.trip.base.mapper.SignMapper;
import cn.wolfcode.trip.base.mapper.SignStateMapper;
import cn.wolfcode.trip.base.mapper.UserIntegralMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ISignService;
import cn.wolfcode.trip.base.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SignServiceImpl implements ISignService {

    @Autowired
    private SignMapper signMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserIntegralMapper userIntegralMapper;
    @Autowired
    private SignStateMapper signStateMapper;

    @Override
    public PageInfo<Sign> addquery(QueryObject qo,Long userId) {
        SignState signState = new SignState();
        SignState state = signStateMapper.selectByPrimaryKey(userId);
        if (state == null){//新增
            signState.setId(userId);
            signState.setWeek(0);
            signState.setHalfMonth(0);
            signState.setMonth(0);
            signStateMapper.insert(signState);
        }
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Sign> list = signMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void save(Long userId) {
        Sign sign = null;
        Sign s = new Sign();
        UserIntegral ui = new UserIntegral();
        //查询最后一天签到是不是昨天,是昨天连续签到就加一,否则就设置为1
        List<Sign> list = signMapper.selectByPrimaryKey(userId);
        UserIntegral userIntegral = userIntegralMapper.selectByPrimaryKey(userId);
        if (list.size() != 0) {
            sign = list.get(0);
            //最后签到时间
            String formatDate = DateUtil.formatDate(sign.getLastDate());
            //现在时间
            Date ago = DateUtil.geDaysAgo(new Date());//获取前一天的天数
            String date = DateUtil.formatDate(ago);
            if (formatDate.equals(date)) {//如果是,就是连续签到
                s.setDays(sign.getDays() + 1);
                s.setContinuousDays(sign.getContinuousDays() + 1);
                s.setLastDate(new Date());
                s.setUser(userMapper.selectByPrimaryKey(userId));
                signMapper.insert(s);

            } else {
                s.setDays(sign.getDays() + 1);
                s.setContinuousDays(1);//连续签到为1
                s.setLastDate(new Date());
                s.setUser(userMapper.selectByPrimaryKey(userId));
                signMapper.insert(s);
            }
            //添加积分
            if (userIntegral != null) {
                ui.setIntegral(userIntegral.getIntegral() + 1000);
                ui.setId(userId);
                userIntegralMapper.updateByPrimaryKey(ui);
            }
        } else {//没有签到过
            s.setDays(1);
            s.setUser(userMapper.selectByPrimaryKey(userId));
            s.setLastDate(new Date());
            s.setDays(1);
            s.setContinuousDays(1);
            signMapper.insert(s);
            //添加积分
            if (userIntegral == null) {
                ui.setId(userId);
                ui.setIntegral(1000);
                userIntegralMapper.insert(ui);
            } else {
                ui.setIntegral(userIntegral.getIntegral() + 1000);
                ui.setId(userId);
                userIntegralMapper.updateByPrimaryKey(ui);
            }


        }


    }
}
